package com.carassistant.beta.compass;
 

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.carassistant.beta.R;
import com.carassistant.beta.common.CarAssistantActivity;

public class CompassMainActivity extends CarAssistantActivity implements SensorEventListener{ 
	private TextView degreeText = null; 
	private String[] directions;   
    private RelativeLayout root;
	
    private Handler handler;
    private static final int UPDATA_DEGREE_FLAG = 1;
    private static final int UPDATA_COMPASS_BG = 2;
    private ImageView test;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.compass_main);
		
		root = (RelativeLayout) findViewById(R.id.root);
		root.addView(getHeaderInstance(CarAssistantActivity.HEAD_HAVE_BACK, "指南针"));
        test = (ImageView) findViewById(R.id.test); 
        
		init();
		handler.sendEmptyMessageDelayed(UPDATA_COMPASS_BG, 500);
	}
	
	private void init(){
		degreeText = (TextView) findViewById(R.id.degreeText); 
		directions = getResources().getStringArray(R.array.direction_text);  
		 
        handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case UPDATA_DEGREE_FLAG:
					degreeText.setText(msg.obj.toString());
					break;
				default:
					break;
				}
			} 
        };
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);  
		List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
		if (sensors == null || sensors.isEmpty()) {
			noSensorDialog();
		}
		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
        		SensorManager.SENSOR_DELAY_FASTEST); 
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		sensorManager.unregisterListener(this);
	}
	
	private void noSensorDialog(){
		Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("");
		dialog.setMessage(getString(R.string.compass_no_sensor));
		dialog.setPositiveButton("Ok", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		dialog.create().show();
	}
	
	private float currentDegree = 0f;
	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { 
			int x = (int) event.values[SensorManager.DATA_X];
			 	
			int temp = x;
			int index = getDirection(temp);  
			handler.obtainMessage(UPDATA_DEGREE_FLAG, directions[index] + "\n" + 
					temp + "°").sendToTarget();     
			
			RotateAnimation ra = new RotateAnimation(currentDegree, -x,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			ra.setDuration(200);
			test.startAnimation(ra); 
			currentDegree = -x;
		}
	} 

	private static final float DEVIDE_DEGREE = 22.5f;
	public int getDirection(float degree) {
		int degreeIndex = (int) (degree / DEVIDE_DEGREE); 
		if (degreeIndex == 16){
			degreeIndex = 15; 
		}
		return degreeIndex;   
	}
	 
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}
}
